package com.firefly.admin.common.config;

import com.firefly.admin.common.overwrite.CustomThreadPoolTaskScheduler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

@Configuration
public class ThreadPoolConfig {
    /**
     * poolSize = 20
     */
    @Value("${spring.thread.pool.poolSize}")
    private int poolSize;
    /**
     * awaitTerminationSeconds = 60
     */
    @Value("${spring.thread.pool.awaitTerminationSeconds}")
    private int awaitTerminationSeconds;
    /**
     * threadNamePrefix = taskExecutor-
     */
    @Value("${spring.thread.pool.threadNamePrefix}")
    private String threadNamePrefix;

    @Bean
    public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
        CustomThreadPoolTaskScheduler executor = new CustomThreadPoolTaskScheduler();
        executor.setPoolSize(poolSize);
        executor.setThreadNamePrefix(threadNamePrefix);
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(awaitTerminationSeconds);
        return executor;
    }
}
